package com.jxc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxc.UI.EasyUIData;
import com.jxc.VO.GoodsVO;
import com.jxc.pojo.Goods;
import com.jxc.service.CustomerReturnListGoodsService;
import com.jxc.service.GoodsService;
import com.jxc.service.GoodsTypeService;
import com.jxc.service.SaleListGoodsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/admin/goods/")
@RestController
public class GoodsAdminController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsTypeService goodsTypeService;
    @Resource
    private SaleListGoodsService saleListGoodsService;
    @Resource
    private CustomerReturnListGoodsService customerReturnListGoodsService;

    @RequestMapping("/listInventory")
    public EasyUIData listInventory(Goods goods, Integer page, Integer rows,String codeOrName,Integer typeId)throws Exception{
        if (page==null)page=1;
        if (rows==null)rows = 20;
        if(codeOrName == null)codeOrName = "";
        if(typeId == null)typeId = 1;
        PageHelper.startPage(page,rows);
        List<GoodsVO> goodsList=goodsService.list(codeOrName);
       // Long total=goodsService.getCount();
        for (GoodsVO g:goodsList
             ) {
            g.setType(goodsTypeService.selectByGoodsId(g.getTypeId()));
            g.setSaleTotal(saleListGoodsService.getTotalByGoodsId(g.getId())-customerReturnListGoodsService.getTotalByGoodsId(g.getId()));
        }
        PageInfo pageInfo = new PageInfo(goodsList);
        List list = pageInfo.getList();
        long total = pageInfo.getTotal();
        EasyUIData easyUIData = new EasyUIData();
        easyUIData.setRows(list);
        easyUIData.setTotal(total);
        return  easyUIData;
    }
}
